package com.tiancheng.trade.settlement.service.settlement.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiancheng.trade.settlement.enums.CostTypeEnum;
import com.tiancheng.trade.settlement.enums.PaymentChannelEnum;
import com.tiancheng.trade.settlement.exception.BusinessException;
import com.tiancheng.trade.settlement.mapper.settlement.SettlementPayAcctConfigMapper;
import com.tiancheng.trade.settlement.model.settlement.SettlementPayAcctConfig;
import com.tiancheng.trade.settlement.service.settlement.ISettlementPayAcctConfigService;
import com.tiancheng.trade.settlement.util.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class SettlementPayAcctConfigService implements ISettlementPayAcctConfigService {

    @Autowired
    private SettlementPayAcctConfigMapper mapper;

    @Override
    public List<SettlementPayAcctConfig> queryList(SettlementPayAcctConfig param) throws BusinessException {
        QueryWrapper<SettlementPayAcctConfig> conditions = new QueryWrapper<>();
        conditions.eq("is_delete", "0");
        if (StringUtils.isNotEmpty(param.getPayChannel())) {
            conditions.eq("pay_channel", param.getPayChannel());
        }
        if (StringUtils.isNotEmpty(param.getCostType())) {
            conditions.eq("cost_type", param.getCostType());
        }
        if (param.getVersion() != null) {
            conditions.eq("version", param.getVersion());
        }
        List<SettlementPayAcctConfig> list = mapper.selectList(conditions);
        list.forEach(config -> {
            config.setPayChannelCn(PaymentChannelEnum.getDesc(config.getPayChannel()));
            config.setDataTypeCn(CostTypeEnum.getDesc(config.getCostType()));
        });
        return list;
    }

    @Override
    public SettlementPayAcctConfig query(Long id) throws BusinessException {

        SettlementPayAcctConfig config = mapper.selectById(id);
        if(null==config){
            throw new RuntimeException("未到该配置");
        }
        config.setPayChannelCn(PaymentChannelEnum.getDesc(config.getPayChannel()));
        config.setDataTypeCn(CostTypeEnum.getDesc(config.getCostType()));
        return config;

    }

    @Override
    public int create(SettlementPayAcctConfig settlementPayAcctConfig) throws BusinessException {
        SettlementPayAcctConfig config = new SettlementPayAcctConfig();
        config.setPayChannel(settlementPayAcctConfig.getPayChannel());
        config.setCostType(settlementPayAcctConfig.getCostType());
        config.setPayAcct(settlementPayAcctConfig.getPayAcct());
        config.setPayAcctName(settlementPayAcctConfig.getPayAcctName());
        config.setColAcct(settlementPayAcctConfig.getColAcct());
        config.setColAcctName(settlementPayAcctConfig.getColAcctName());
        config.setRemark(settlementPayAcctConfig.getRemark());
        config.setCreatedDt(LocalDateTime.now());
        config.setCreatedBy(UserUtils.currentUserId());
        return mapper.insert(config);
    }

    @Override
    public int update(SettlementPayAcctConfig settlementPayAcctConfig) throws BusinessException {
        SettlementPayAcctConfig config = new SettlementPayAcctConfig();
        config.setId(settlementPayAcctConfig.getId());
        config.setCostType(settlementPayAcctConfig.getCostType());
        config.setPayAcct(settlementPayAcctConfig.getPayAcct());
        config.setPayAcctName(settlementPayAcctConfig.getPayAcctName());
        config.setColAcct(settlementPayAcctConfig.getColAcct());
        config.setColAcctName(settlementPayAcctConfig.getColAcctName());
        config.setRemark(settlementPayAcctConfig.getRemark());
        config.setLastUpdDt(LocalDateTime.now());
        config.setLastUpdBy(UserUtils.currentUserId());
        return mapper.updateById(config);
    }


    @Override
    public int remove(Long id) throws BusinessException {
        SettlementPayAcctConfig config = new SettlementPayAcctConfig();
        config.setLastUpdDt(LocalDateTime.now());
        config.setId(id);
        config.setIsDelete(1);
        config.setCreatedDt(null);
        config.setLastUpdBy(UserUtils.currentUserId());
        QueryWrapper<SettlementPayAcctConfig> conditions = new QueryWrapper<>();
        conditions.eq("id", id);
        return mapper.update(config, conditions);
    }

    @Override
    public List<SettlementPayAcctConfig> queryConfigByCostType(SettlementPayAcctConfig param) throws BusinessException {
        QueryWrapper<SettlementPayAcctConfig> conditions = new QueryWrapper<>();
        conditions.eq("is_delete", "0");
        if (StringUtils.isNotEmpty(param.getCostType())) {
            conditions.eq("cost_type", param.getCostType());
        }
        if (null != param.getId()) {
            conditions.ne("id", param.getId());
        }
        List<SettlementPayAcctConfig> list = mapper.selectList(conditions);
        list.forEach(config -> {
            config.setPayChannelCn(PaymentChannelEnum.getDesc(config.getPayChannel()));
            config.setDataTypeCn(CostTypeEnum.getDesc(config.getCostType()));
        });
        return list;
    }

}
